

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dataMgr.Group;
import dataMgr.GroupManager;
import dataMgr.Lib;
import dataMgr.LibManager;

/**
 * Servlet implementation class CheckHashServlet
 */
public class CheckHashServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckHashServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	System.out.println("check hash servlet");
		response.setContentType("text/plain");
		String hash=request.getParameter("hash");
		Long num = Long.parseLong(request.getParameter("num"));
		String fileName = request.getParameter("fileName");
		
		synchronized(ConnectionsHolder.getInstance().last_group)
		{
			ConnectionsHolder.getInstance().last_group=num;
		}
		Group group ;
		
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
		Session session = sessionFactory.openSession();

		Query query = session.createQuery("from Lib g where g.hashCode like '"+hash+"'");

		java.util.List <Lib>list = query.list();
		if(list.isEmpty())
		{
			System.out.println("emty");
		}
		
		
		
		LibManager manager = new LibManager(session);
		Lib lib = new Lib();
		lib.setFileName(fileName);
		lib.setGroupID(num);
		lib.setHashCode(hash);
		
		
		//manager.saveLib(lib);
		session.flush();
		PrintWriter out =new PrintWriter(response.getOutputStream());

		Boolean b=list.isEmpty();
		
		if(b)
		{
			Query query1 = session.createQuery("select max(g.fileID) from Lib g ");
			java.util.List <Long>list1 = query1.list();
			Long max;
			if(list1==null || list1.isEmpty())
				max=(long) 0;
			else
				max= list1.get(0);
				
			if(max==null)
				max=(long) 0;
			lib.setFileID(max+1);

		}
		else
		{
			lib.setFileID(list.get(0).getFileID());
		}
		
		 manager.saveLib(lib);
		 session.flush();
		 
		out.print(b.toString());
		out.flush();
		out.close();
		
		/*synchronized(ConnectionsHolder.getInstance().last_group)
		{
			ConnectionsHolder.getInstance().last_group=num;
		}*/
	}

}
